#' 增加图片
#'
#' @param doc R1_start_docx功能生成的doc
#' @param figure 外部图片，ggplot图片或R base的基本绘图
#' @param style "centered"
#' @param width 图片宽度，默认6
#' @param height 图片高度，默认5
#' @param res 图片精度，默认300
#' @param scale 缩放比例
#' @param add_break 是否在图片后添加分页，默认是
#'
#' @return 添加了图片的docx
#' @export
#'
#' @examples
#'
#' \dontrun{
#' # 外部图片文件
#' img.file <- file.path( R.home("doc"), "html", "logo.jpg" )
#'
#'
#'  require("ggplot2")
#'
#' # ggplot2图片
#' require("ggplot2")
#' gg <- ggplot(data = iris) +
#'  geom_point(mapping = aes(Sepal.Length, Petal.Length))
#'
#'
#' # R Base 基本 plot
#'  require("officer")
#' p <-  plot_instr(
#'   code = {
#'     barplot(1:5, col = 2:6)  # R Base 基本绘图代码，放在plot_instr功能里
#'   }
#' )
#'
#' # install.packages("mschart")
#' require(mschart)
#'
#' my_barchart <- ms_barchart(data = browser_data,
#'                            x = "browser", y = "value", group = "serie")
#' my_barchart <- chart_settings( x = my_barchart,
#'                                dir="vertical", grouping="clustered", gap_width = 50 )
#'
#' scatter <-
#'   ms_scatterchart(
#'     data = iris, x = "Sepal.Length",
#'     y = "Sepal.Width", group = "Species"
#'   )
#' scatter <- chart_settings(scatter, scatterstyle = "marker")
#'
#'
#'
#' doc <- R1_start_docx() %>%
#' R4_add_figure(img.file) %>%
#'   R4_add_figure(gg) %>%
#'   R4_add_figure(p)%>%
#'  R4_add_figure(my_barchart)%>%
#'   R4_add_figure(scatter)
#'
#' # 输出到example.docx查看效果
#' print(doc, target = "example.docx" )
#'
#'
#'
#' # 删除example.docx
#' unlink("example.docx")
#'
#'
#' }
#'
#'
#'
#'
#'
R4_add_figure<-function(doc = doc,
                        figure=figure,
                        style = "centered",
                        width =6 ,
                        height = 5,
                        res=300,
                        scale = 1,
                        add_break=TRUE){

  require(officer)

  if( is.character(figure) ){

    if(file.exists(figure)) {
      class(figure)<-c("img",class(figure))
    }

  }


  doc<- add_figure(figure=figure,
                   doc = doc,
                   style = style,
                   width = width,
                   height =height,
                   res = res,
                   scale = scale)

  if(add_break){
    doc <- doc %>%
      body_add_break()
  }

  return(doc)
}

add_figure<-function(figure=figure,
                     doc = doc,
                     style = style,
                     width = width,
                     height =height,
                     res = res,
                     scale = scale){
  UseMethod("add_figure")
}


add_figure.img<-function(figure=figure,
                         doc = doc,
                         style = style,
                         width = width,
                         height =height,
                         res = res,
                         scale = scale){
  require(officer)
  doc <- doc %>%
    body_add_img(  src=figure ,
                   style=style,
                   width = width,
                   height =height)
  return(doc)
}

add_figure.ggplot<-function(figure=figure,
                            doc = doc,
                            style = style,
                            width = width,
                            height =height,
                            res = res,
                            scale = scale){
  require(officer)
  doc <- doc %>%
    body_add_gg(  value=figure ,
                  style=style,
                  width = width,
                  height =height,
                  res = res,
                  scale = scale)
  return(doc)
}


add_figure.plot_instr<-function(figure=figure,
                                doc = doc,
                                style = style,
                                width = width,
                                height =height,
                                res = res,
                                scale = scale){
  require(officer)
  doc <- doc %>%
    body_add_plot(  value=figure ,
                    style=style,
                    width = width,
                    height =height,
                    res = res)
  return(doc)
}

add_figure.ms_chart<-function(figure=figure,
                              doc = doc,
                              style = style,
                              width = width,
                              height =height,
                              res = res,
                              scale = scale){
  require(mschart)
  doc <- doc %>%
    body_add_chart( chart=figure ,
                    style=style,
                    width = width,
                    height =height)
  return(doc)
}

add_figure.default<-function(figure=figure,
                            doc = doc,
                            style = style,
                            width = width,
                            height =height,
                            res = res,
                            scale = scale){
  require(officer)
  require(ggplot2)

  file <- tempfile(fileext = ".png")
  ggsave( filename =file,plot = figure,scale = scale,width = width,height = height,units = "in",dpi = res,limitsize = F,bg="white"  )

  doc <- doc %>%
    body_add_img(  src = file ,
                  style=style,
                  width = width,
                  height =height)
  unlink(file)
  return(doc)
}
